package cn.lanqiao.ui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;

import cn.lanqiao.entity.Users;

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;

public class CheckFrm extends JFrame {

	private static final long serialVersionUID = 1L;
	
	private JLabel lblMobileNum;
	private JLabel lblVerificationNum;
	private JTextField textMobileNum;
	private JTextField textVerificationNum;
	private JButton btnSendOut;
	private JButton btnOK;
	private String verifyCode = "";
	private String telephone= "";
	
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CheckFrm frame = new CheckFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public CheckFrm() {
		setTitle("手机号码验证");
		setResizable(false);
		setBounds(100, 100, 400, 250);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//窗体居中
		setLocationRelativeTo(null);
		getContentPane().setLayout(null);
		
		lblMobileNum = new JLabel("手机号码");
		lblMobileNum.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		lblMobileNum.setBounds(40, 40, 90, 30);
		getContentPane().add(lblMobileNum);
		
		textMobileNum = new JTextField();
		textMobileNum.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		textMobileNum.setBounds(140, 40, 200, 30);
		getContentPane().add(textMobileNum);
		textMobileNum.setColumns(10);
		
		lblVerificationNum = new JLabel("验 证 码");
		lblVerificationNum.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		lblVerificationNum.setBounds(40, 100, 90, 30);
		getContentPane().add(lblVerificationNum);
		
		textVerificationNum = new JTextField();
		textVerificationNum.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		textVerificationNum.setBounds(140, 100, 200, 30);
		getContentPane().add(textVerificationNum);
		textVerificationNum.setColumns(10);
		
		btnSendOut = new JButton("发送验证码");
		btnSendOut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//短信验证
				telephone = textMobileNum.getText();
				cn.lanqiao.service.UserService service = new cn.lanqiao.service.impl.UserServiceImpl();
				Users users = service.send(telephone);
				if( users == null ) {
					textMobileNum.setText(null);
					JOptionPane.showMessageDialog(null, "手机号码错误，请重新输入！");
				}else {
					verifyCode = service.Verification();
					String message ="您的验证码："+verifyCode+"\t\n"+"如非本人操作，请忽略此短信！";
					JOptionPane.showMessageDialog(null, message);
				}
				
			}
		});
		btnSendOut.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		btnSendOut.setBounds(50, 160, 140, 30);
		getContentPane().add(btnSendOut);
		
		btnOK = new JButton("确定");
		btnOK.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String Verification = textVerificationNum.getText();
				if(CheckFrm.this.verifyCode.equals(Verification)) {
					
					ChangePassFrm changeFrm = new ChangePassFrm(telephone); 
					changeFrm.setVisible(true);
					CheckFrm.this.setVisible(false);
				}else {
					JOptionPane.showMessageDialog(null, "验证码出错！");
				}
			
			}
		});
		btnOK.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		btnOK.setBounds(220, 160, 120, 30);
		getContentPane().add(btnOK);
		
		JLabel lblSex = new JLabel();
		lblSex.setIcon(new ImageIcon("images\\sex.gif"));
		lblSex.setBounds(0, 0, 394, 215);
		getContentPane().add(lblSex);
	}
}
